package com.yunmai.cloud.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.yunmai.cloud.web.domain.RealDatasEntity;
import com.yunmai.cloud.web.dto.realdatas.response.RealDatasResponseDto;
import com.yunmai.cloud.web.services.IRealDatasService;
import com.yunmai.core.spring.model.GridResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;

/**
 * <p>
 *  实时数据查询Controller
 * </p>
 *
 * @author 赵刚
 * @since 2023-01-11
 **/
@RestController
@Validated
@RequestMapping("/v1")
public class RealDatasController {

    @Autowired
    private IRealDatasService realDatasService;

    static SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @ApiOperation(
            value = "获取实时数据",
            notes = "获取实时数据",
            produces = "application/json",
            consumes = "application/json",
            response = GridResult.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "station_id", value = "命令id", required = true, dataType = "Long", example = "1")
    })
    @GetMapping(value = "/real_datas/{station_id}")
    public ResponseEntity<?> getRealDatasByStationId(
            @PathVariable(value = "station_id", required = true)
                Long stationid) {

        RealDatasEntity realDatasEntity = realDatasService.getByStationId(stationid);
        RealDatasResponseDto realDatasResponseDto = new RealDatasResponseDto();
        realDatasResponseDto.setStationId(realDatasEntity.getStationId());
        realDatasResponseDto.setDataId(realDatasEntity.getDataId());
        realDatasResponseDto.setValue(JSONObject.parseObject(realDatasEntity.getValue()));
        realDatasResponseDto.setCreatedTime(sdf.format(realDatasEntity.getCreatedTime()));
        return ResponseEntity.ok(realDatasResponseDto);
    }
}